using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 用户设备管理接口。
    /// 提供用户设备的增删改查相关 API。
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class UserDeviceController : ControllerBase
    {
        /// <summary>
        /// 用户设备应用服务接口
        /// </summary>
        private readonly IUserDeviceAppService _service;
        
        /// <summary>
        /// 构造函数，注入用户设备服务
        /// </summary>
        /// <param name="service">用户设备应用服务接口</param>
        public UserDeviceController(IUserDeviceAppService service)
        {
            _service = service;
        }
        
        /// <summary>
        /// 获取所有用户设备信息
        /// </summary>
        /// <returns>用户设备列表</returns>
        [HttpGet]
        public async Task<IEnumerable<UserDeviceDto>> GetAll()
        {
            return await _service.GetAllAsync();
        }
        
        /// <summary>
        /// 根据主键获取用户设备信息
        /// </summary>
        /// <param name="id">设备主键</param>
        /// <returns>用户设备数据</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<UserDeviceDto>> GetById(long id)
        {
            var result = await _service.GetByIdAsync(id);
            if (result == null) return NotFound();
            return result;
        }
        
        /// <summary>
        /// 添加用户设备信息
        /// </summary>
        /// <param name="dto">用户设备数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> Add(UserDeviceDto dto)
        {
            await _service.AddAsync(dto);
            return Ok();
        }
        
        /// <summary>
        /// 更新用户设备信息
        /// </summary>
        /// <param name="id">设备主键</param>
        /// <param name="dto">用户设备数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(long id, UserDeviceDto dto)
        {
            await _service.UpdateAsync(id, dto);
            return Ok();
        }
        
        /// <summary>
        /// 删除用户设备信息
        /// </summary>
        /// <param name="id">设备主键</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            await _service.DeleteAsync(id);
            return Ok();
        }
    }
}
